package common;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.MainActivity;
import com.concerneyes.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Depiction: 抽象activity界面，为了适配SsiwoFacenginActivity单独列出来的父类
 */
public abstract class AbsActivityForSsiwo extends BaseActivityForSsiwo {
    @Bind(R.id.back_btn)
    ImageButton backBtn;
    @Bind(R.id.left_btn)
    ImageButton leftBtn;
    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.right_btn)
    ImageButton rightBtn;
    @Bind(R.id.right_btn2)
    ImageButton rightBtn2;
    @Bind(R.id.title_bar)
    RelativeLayout titleBar;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.right_text2)
    TextView rightText2;
    @Bind(R.id.content_layout)
    FrameLayout rootLayout;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        windowSet();
        setCancelable(false);
        setContentView(R.layout.activity_abs);
        ButterKnife.bind(this);
        if (this instanceof MainActivity) {
            backBtn.setVisibility(View.INVISIBLE);
        }
        CEApp.getInstance().addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void openActivity(Class<?> activity, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, activity);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void setRootView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    public void setRootView(View view) {
        super.setContentView(view);
    }

    public void setRootView(View view, LayoutParams params) {
        super.setContentView(view, params);
    }

    @Override
    public void setContentView(int layoutResID) {
        if (rootLayout != null) {
            View child = LayoutInflater.from(getApplicationContext()).inflate(layoutResID, null);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, -1);
            rootLayout.addView(child, params);
        } else {
            super.setContentView(layoutResID);
        }
    }

    @Override
    public void setContentView(View view) {
        if (rootLayout != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, -1);
            rootLayout.addView(view, params);
        } else {
            super.setContentView(view);
        }
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        if (rootLayout != null) {
            rootLayout.addView(view, params);
        } else {
            super.setContentView(view, params);
        }
    }

    @Override
    public View findViewById(int id) {
        if (rootLayout != null) {
            return rootLayout.findViewById(id);
        } else {
            return super.findViewById(id);
        }
    }

    public ImageButton getBackButton() {
        return backBtn;
    }

    public ImageButton getLeftBtn() {
        return leftBtn;
    }

    public TextView getLeftText() {
        return leftText;
    }

    public TextView getTitleView() {
        return titleView;
    }

    public ImageButton getRightBtn() {
        return rightBtn;
    }

    public ImageButton getRightBtn2() {
        return rightBtn2;
    }

    public TextView getRightText() {
        return rightText;
    }

    public TextView getRightText2() {
        return rightText2;
    }

    public RelativeLayout getTitleBar() {
        return titleBar;
    }

    public void onBackAction(View v) {
        onBackPressed();
    }

    public void onLeftAction(View v) {
    }

    public void onLeftTextAction(View v) {
    }

    public void onRightAction(View v) {
    }

    public void onRight2Action(View v) {
    }

    public void onRightTextAction(View v) {
    }

    public void onRightText2Action(View v) {
    }

    public void onTitleAction(View v) {
    }

    @Override
    public void setTitle(CharSequence title) {
        titleView.setText(title);
        super.setTitle(title);
    }

    @Override
    public void setTitle(int titleId) {
        titleView.setText(titleId);
        super.setTitle(titleId);
    }

    public TextView getTitleText() {
        return titleView;
    }

    public String getActivityTitle() {
        return titleView.getText().toString();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    //设置标题栏布局为透明，并增加一个与状态栏高度一样的空白布局
    public void windowSet() {
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = getStatusBarHeight();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundResource(R.drawable.shade);
        decorViewGroup.addView(statusBarView);
        Log.e(this.getClass().getSimpleName(), "getWindow()==>" + getWindow());
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            return getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    public void setWindowStatusBarColor() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(R.color.app_blue));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
